#define ENABLE_TEST_RENDERER

#include "gl_renderer.h"
#include "gl_inc.h"
#include "gl_texture_mgr.h"
#include "gl_light.h"

#include "model_data/md_ver_1_0.h"

#ifdef ENABLE_TEST_RENDERER
void TestRendererUpdate();
void TestRendererInit();
#endif

GlRenderer::GlRenderer(void)
{
	LOG("hello gl_renderer ......\n");

	//actMatAmbient = glm::vec4(1.0,2.0,3.0,4.0);
}

GlRenderer::~GlRenderer(void)
{

}

void GlRenderer::SetSurfaceMaterialProperty(glm::vec4 *ambient, glm::vec4 *diffuse, glm::vec4 *specular, glm::vec2 *shine, glm::vec4 *emission, bool force)
{
	if(force == true || (ambient != 0 && *ambient != actMatAmbient) )
	{
		actMatAmbient = *ambient;
		glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&actMatAmbient);
	}
	if(force == true || (diffuse != 0 && *diffuse != actMatDiffuse) )
	{
		actMatDiffuse = *diffuse;
		glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&actMatDiffuse);
	}
	if(force == true || (specular != 0 && *specular != actMatSpecular) )
	{
		actMatSpecular = *specular;
		glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&actMatSpecular);
	}
	if(force == true || (shine != 0 && *shine != actMatShine) )
	{
		actMatShine = *shine;
		glMaterialfv(GL_FRONT, GL_SHININESS, (float *)&actMatShine);
	}
	if(force == true || (emission != 0 && *emission != actMatEmission) )
	{
		actMatEmission = *emission;
		glMaterialfv(GL_FRONT,  GL_EMISSION, (float *)&actMatEmission);
	}
}


void GlRenderer::Resize(int32 width, int32 height)
{
	LOG("%l %l \n ", width, height);
	glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  /* modify this line to change perspective values */
    gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

#ifdef ENABLE_TEST_RENDERER
   TestRendererInit();
#endif
}


void GlRenderer::Update()
{
	//begin rendering
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

#ifdef ENABLE_TEST_RENDERER
	TestRendererUpdate();
#endif

	glFlush();

	V3dRenderer::Update();
}

void GlRenderer::InitRenderer()
{
	textureMgr = new GlTextureMgr();

	lightMgr = new GLLightMgr();
}

int	GlRenderer::Render(ModelData *md)
{
	switch(md->modelDataVersion)
	{
	case MD_VER_1_0:
		{
			if(((MDVer1_0 *)md)->totalMeshes != 0)
			{
				for(int meshId =0; meshId < ((MDVer1_0 *)md)->totalMeshes; meshId ++)
				{
					//@WorkAtProgress
					//everytime calling get texture as model class is not ready... no ptr to matrial or texutre
					char textureName[100];
					sprintf(textureName,"%s.bmp",((MDVer1_0 *)md)->meshes[meshId].materialName);

					textureMgr->BindTexture(textureName);

					glInterleavedArrays( ((MDVer1_0 *)md)->format, ((MDVer1_0 *)md)->stride, ((MDVer1_0 *)md)->data );
					glDrawArrays(  ((MDVer1_0 *)md)->primitive, ((MDVer1_0 *)md)->meshes[meshId].dataOffset, ((MDVer1_0 *)md)->meshes[meshId].size );
				}
			}
			else
			{
				glInterleavedArrays( ((MDVer1_0 *)md)->format, ((MDVer1_0 *)md)->stride, ((MDVer1_0 *)md)->data );
				glDrawArrays( GL_TRIANGLES, 0, ((MDVer1_0 *)md)->size );
			}
		}
		break;
	}
	return SUCCESS;
}





